package org.xfy.org.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.xfy.org.query.DepartmentQuery;
import org.xfy.org.domain.Department;
import org.xfy.org.service.IDepartmentService;
import org.xfy.base.util.AjaxResult;
import org.xfy.base.util.PageList;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/dept")
@Api(value = "部门的API", description = "部门相关的CRUD功能")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    @PostMapping
    @ApiOperation(value = "新增或者修改")
    public AjaxResult save(@Valid @RequestBody Department department) {
        if (department.getId() == null) {//新增
            departmentService.add(department);
        } else {
            departmentService.update(department);
        }
        return AjaxResult.me();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过id删除")
    public AjaxResult del(@PathVariable("id") Long id) {
        departmentService.del(id);
        return AjaxResult.me();
    }

    @PatchMapping()
    @ApiOperation(value = "批量删除")
    public AjaxResult PatchDel(@RequestBody Long[] ids) {
        departmentService.patchDel(ids);
        return AjaxResult.me();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询")
    public AjaxResult findOne(@PathVariable("id") Long id) {
        Department department = departmentService.findOne(id);
        return AjaxResult.me().setResultObj(department);
    }

    @GetMapping()
    @ApiOperation(value = "查询所有")
    public AjaxResult findAll() {
        List<Department> departments = departmentService.findAll();
        return AjaxResult.me().setResultObj(departments);
    }

    @PostMapping("/pageList")
    @ApiOperation(value = "分页查询")
    public AjaxResult pageList(@RequestBody DepartmentQuery query) {
        PageList<Department> pageList = departmentService.queryPage(query);
        return AjaxResult.me().setResultObj(pageList);
    }

    @GetMapping("/deptsTree")
    @ApiOperation(value = "部门树")
    public AjaxResult deptsTree() {
        List<Department> deptsTrees = departmentService.deptsTree();
        return AjaxResult.me().setResultObj(deptsTrees);
    }
}
